package utils;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;


/**
 * @Description: spring security自带的密码加密工具
 * @date: 2023/6/19 9:12
 * @author: Huangjunjie
 * @since JDK 1.8
*/
public class HashPwd {

    public static void main(String[] args) {
        //1.加密方法
        String password = "Admin@2023";
//        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encodedPassword = new BCryptPasswordEncoder().encode(password);
        System.out.println(encodedPassword);


        //2.密码匹配方法
        String passwordFromUser = "$2a$10$c0P42qNi7hoZDQ2.O8rO2.rLqSp5QIn5jxcvOeBWUqkKgJ4s.9Y1K";  // 用户输入的密码
        //String encodedPasswordFromDB = "$2a$10$DuRQjf7Xsl529q5PAL06HOf5d6Iqjb/jXdhf67CDhSmnPo.rCOw12";  // 数据库中存储的加密密码

        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        boolean isMatched = encoder.matches(password, passwordFromUser);

        if (isMatched) {
            // 密码匹配成功
            System.out.println("密码匹配成功");
        } else {
            // 密码不匹配
            System.out.println("密码不匹配");
        }

    }
}
